package com.drink.yan.listener;

import com.alibaba.fastjson.JSONObject;
import com.drink.yan.domain.YanUserDo;
import com.drink.yan.intf.define.YanOpSourceStatusE;
import com.drink.yan.svc.service.YanUserService;
import com.drink.yan.web.rest.req.cmd.AddStudentInfoCmd;
import com.github.meixuesong.aggregatepersistence.Aggregate;
import com.meta.framework.act.entity.SysUser;
import com.meta.framework.domain.event.DomainEvent;
import com.meta.platform.login.plus.vcode.event.WeixinLoginUserEvent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * java类简单作用描述
 *
 * @Title:
 * @Package: com.drink.yan.listener.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2024-04-24 14:56
 * @Version: v2.0
 */
@Slf4j
@Component
public class WeixinLoginUserListener{

	@Resource
	private YanUserService yanUserService;

	@EventListener
	//@Async
	@SneakyThrows
	public void handle(WeixinLoginUserEvent weixinLoginUserEvent) {

		//Thread.sleep(10000L);

		try {
			SysUser sysUser = weixinLoginUserEvent.getSysUser();
			log.info("=========== login notify event , req :{}===============", JSONObject.toJSONString(sysUser));
			Aggregate<YanUserDo> yanUserDo = yanUserService.getYanUserDo(sysUser.getUserId());
			YanUserDo root = yanUserDo.getRoot();
			if(Objects.nonNull(root) && (Objects.isNull(root.getYanUserStudent()) || Objects.isNull(root.getYanUserStudent().getUserId()))){
				AddStudentInfoCmd addStudentInfoCmd = new AddStudentInfoCmd();
				addStudentInfoCmd.setUserId(sysUser.getUserId());
				addStudentInfoCmd.setUserName(sysUser.getNickName());
				addStudentInfoCmd.setMobile(sysUser.getPhonenumber());
				addStudentInfoCmd.setOpSource(YanOpSourceStatusE.student_mini.getCode());
				yanUserService.saveStudentInfo(addStudentInfoCmd);
			}
		}catch (Exception e){
			log.error("========WeixinLoginUserListener catch error=======", e);
		}
	}

}
